package com.example.tests;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertThat;

import com.example.fw.AccountHelper;
import com.example.fw.AdminHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;

public class SignupTest extends TestBase {
/*
	public User user = new User().setLogin(app.getProperty("testUser.Login"))
			.setPassword(app.getProperty("testUser.pass"))
			.setEmail(app.getProperty("testUser.email"));
*/
	public User user = new User().setLogin("testUser22")
			.setPassword("123")
			.setEmail("testuser22@localhost");
	private AccountHelper accHelper;
	private JamesHelper james;
	private AdminHelper admin;	
	
	@BeforeClass
	public void createMailUser(){

		if (!app.getJamesHelper().doesUserExist(user.login))
			app.getJamesHelper().createUser(user.login, user.password);
	}
	
	@Test
	public void newUserShouldSignup() throws Exception{
		accHelper = app.getAccountHelper();
		admin = app.AdminHelper();
		james = app.getJamesHelper();
		admin.deleteUserIfExists(user);
		
		app.getAccountHelper().signup(user);
		app.getAccountHelper().login(user);
		assertThat(app.getAccountHelper().loggedUser(user), equalTo(user.login));
		
	}

	// @Test
	public void existingUserShouldNotSignup() throws Exception{
		//if ( !admin.userExists(user) ){
		//	admin.createUser(user);
		//}
		
		try{
			app.getAccountHelper().signup(user);
		}
		catch (Exception e){
			assertThat(e.getMessage(), containsString("That username is already being used"));
		}
	}

	@AfterClass
	public void deleteMailUser(){
		if (app.getJamesHelper().doesUserExist(user.login))
			app.getJamesHelper().deleteUser(user.login);
	}

}
